package com.wnhz.work.yz.fragment.industry;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.IndustryFitmentActivity;
import com.wnhz.work.yz.base.BaseFragment;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.FitmentIndustryBean;
import com.wnhz.work.yz.bean.IndustryDataBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * ---公寓
 */
public class ApartmentFragment extends BaseFragment {
    public static ApartmentFragment newInstance() {
        return new ApartmentFragment();
    }

    private List<IndustryDataBean> dataList;
    private IndustryFitmentActivity activity;

    private RecyclerView recyclerView;
    private BaseRVAdapter<FitmentIndustryBean> adapter;

    private List<FitmentIndustryBean> beanList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        activity = (IndustryFitmentActivity) getActivity();
        return inflater.inflate(R.layout.fragment_apartment, null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        adapter = new BaseRVAdapter<FitmentIndustryBean>(getActivity(), beanList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.industry_item;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                ImageView iv_img = holder.getView(R.id.iv_img);
                x.image().bind(iv_img, beanList.get(position).getIndex_pic());
                holder.setTextView(R.id.tv_name, beanList.get(position).getGoods_name());
                holder.setTextView(R.id.tv_content, beanList.get(position).getGoods_des());
            }
        };

        recyclerView.setAdapter(adapter);
        upload(0);
        slectInt(0);
    }

    private static final String TAG = "ApartmentFragment";

    public void upload(int s) {
        Map<String, Object> map = new HashMap<>();
        map.put("cateId", dataList.get(s).getCateid());
        XUtil.Post(Url.PACKAGE_GETPACKAGELIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:AGELIST== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    beanList.clear();
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            FitmentIndustryBean bean = new FitmentIndustryBean();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), FitmentIndustryBean.class);

                            beanList.add(bean);
                        }
                    }
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void slectInt(int position) {

        for (int i = 0; i < dataList.size(); i++) {
            if (position == i) {
                dataList.get(i).setSelector(true);

            } else {
                dataList.get(i).setSelector(false);
            }
        }

    }

    public void setData(List<IndustryDataBean> dataList) {
        this.dataList = dataList;
    }
}
